package vip.liux.front.infrastructure.utils;

import org.springframework.util.Assert;

import java.util.function.Consumer;

public class CloseableAction<T> implements AutoCloseable {


    private final Consumer<T> action;

    private final T parameter;

    public CloseableAction(Consumer<T> action, T parameter) {
        Assert.notNull(action, "action");

        this.action = action;
        this.parameter = parameter;
    }

    @Override
    public void close() throws Exception {
        if (parameter != null) {
            action.accept(parameter);
        }
    }
}
